
package com.hd2tecnologia.telas;

import com.hd2tecnologia.entidades.ItensVenda;
import com.hd2tecnologia.entidades.Venda;
import com.hd2tecnologia.modeloBanco.VendaBD;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Dulcyelliton Silva
 */
public class ConsultaVendaTela extends javax.swing.JInternalFrame {

    private DefaultTableModel tableModelVenda;
    private DefaultTableModel tableModelItensVenda;
    private List<Venda> listaVenda;
    private List<ItensVenda> listaItensVenda;
    private ListSelectionModel listModelVenda;

    /** Creates new form ClienteFrame */
    public ConsultaVendaTela() {
        initComponents();
        defineModelo();
    }

    private void defineModelo() {
        tableModelVenda = (DefaultTableModel) tblVendas.getModel();
        tableModelItensVenda = (DefaultTableModel) tblItensVenda.getModel();
        listModelVenda = tblVendas.getSelectionModel();
        listModelVenda.addListSelectionListener(new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent e) {
                if (!e.getValueIsAdjusting()) {
                    mostraDetalheVenda();
                }
            }
        });
        try {
            tblItensVenda.getColumnModel().getColumn(0).setPreferredWidth(400);
            tblVendas.getColumnModel().getColumn(1).setPreferredWidth(500);
        } catch (Exception ex) {
            Logger.getLogger(RegistraVendaTela.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void atualizaTabela() {
        VendaBD vendaBD = new VendaBD();
        java.sql.Date dataInicial = new java.sql.Date(((java.util.Date) ftfDataInicial.getValue()).getTime());
        java.sql.Date dataFinal = new java.sql.Date(((java.util.Date) ftfDataFinal.getValue()).getTime());
        listaVenda = vendaBD.consultaVendaPeriodo(dataInicial, dataFinal);

        int numeroLinhas = tblVendas.getRowCount();
        for (int i = 0; i < numeroLinhas; i++) {
            tableModelVenda.removeRow(0);
        }
        for (int i = 0; i < listaVenda.size(); i++) {
            tableModelVenda.insertRow(i, new Object[]{listaVenda.get(i).getId(),
                        listaVenda.get(i).getCliente().getNome(),
                        listaVenda.get(i).getDataVenda()
                    });
        }
    }

    private void mostraDetalheVenda() {
        if (tblVendas.getSelectedRow() != -1) {
            listaItensVenda = listaVenda.get(tblVendas.getSelectedRow()).getItensVenda();
            removeItensVenda();
            for (int i = 0; i < listaItensVenda.size(); i++) {
                tableModelItensVenda.insertRow(i, new Object[]{listaItensVenda.get(i).getProduto().getNome(),
                            listaItensVenda.get(i).getQtde(),
                            listaItensVenda.get(i).getProduto().getValor(),
                            listaItensVenda.get(i).getProduto().getValor() * listaItensVenda.get(i).getQtde()
                        });
            }
        } else {
            removeItensVenda();
        }
    }

    private void removeItensVenda() {
        int numeroLinhas = tblItensVenda.getRowCount();
        for (int i = 0; i < numeroLinhas; i++) {
            tableModelItensVenda.removeRow(0);
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblItensVenda = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        ftfDataFinal = new javax.swing.JFormattedTextField();
        btnPesquisar = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        ftfDataInicial = new javax.swing.JFormattedTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblVendas = new javax.swing.JTable();
        jLabel4 = new javax.swing.JLabel();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("DSControl - Sistema de Gestão de Loja");
        setPreferredSize(new java.awt.Dimension(600, 400));
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jPanel1.setBackground(java.awt.SystemColor.inactiveCaption);
        jPanel1.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Consultar Venda");
        jPanel1.add(jLabel1);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        getContentPane().add(jPanel1, gridBagConstraints);

        jPanel3.setLayout(new java.awt.GridBagLayout());

        tblItensVenda.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Produto", "Qtde", "Vlr. Unit.", "Vlr. Total"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Double.class, java.lang.Double.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        tblItensVenda.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(tblItensVenda);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weighty = 1.0;
        jPanel3.add(jScrollPane1, gridBagConstraints);

        jLabel3.setText("Data Final");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel3.add(jLabel3, gridBagConstraints);

        ftfDataFinal.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(java.text.DateFormat.getDateInstance(java.text.DateFormat.MEDIUM))));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel3.add(ftfDataFinal, gridBagConstraints);

        btnPesquisar.setText("Pesquisar");
        btnPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPesquisarActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel3.add(btnPesquisar, gridBagConstraints);

        jLabel2.setText("Data Inicial");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel3.add(jLabel2, gridBagConstraints);

        ftfDataInicial.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(java.text.DateFormat.getDateInstance(java.text.DateFormat.MEDIUM))));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel3.add(ftfDataInicial, gridBagConstraints);

        tblVendas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Cliente", "Data Venda"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, true, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(tblVendas);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        jPanel3.add(jScrollPane2, gridBagConstraints);

        jLabel4.setText("Itens Venda");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel3.add(jLabel4, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weighty = 1.0;
        getContentPane().add(jPanel3, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPesquisarActionPerformed
        if (ftfDataInicial.getValue() == null) {
            JOptionPane.showMessageDialog(this, "Informe a data inicial!", "Erro", JOptionPane.ERROR_MESSAGE);
            ftfDataInicial.requestFocus();
        } else if (ftfDataFinal.getValue() == null) {
            JOptionPane.showMessageDialog(this, "Informe a data final!", "Erro", JOptionPane.ERROR_MESSAGE);
            ftfDataFinal.requestFocus();
        } else {
            atualizaTabela();
        }
    }//GEN-LAST:event_btnPesquisarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnPesquisar;
    private javax.swing.JFormattedTextField ftfDataFinal;
    private javax.swing.JFormattedTextField ftfDataInicial;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tblItensVenda;
    private javax.swing.JTable tblVendas;
    // End of variables declaration//GEN-END:variables
}
